package com.jeeplus.modules.monitor.task;

import com.jeeplus.modules.ambassadorrewards.entity.AmbassadorRewards;
import com.jeeplus.modules.ambassadorrewards.service.AmbassadorRewardsService;
import com.jeeplus.modules.api.common.UUIDUtils;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;

import com.jeeplus.modules.monitor.entity.Task;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Slf4j
@DisallowConcurrentExecution
public class TestTask extends Task {

    @Autowired
    private MemberService memberService;

    @Autowired
    private AmbassadorRewardsService ambassadorRewardsService;

    @Override
    public void run() {
        // 每月1号执行大使分红
        Member member = new Member();
        member.setVipType("8");
        List<Member> list = memberService.findList(member);
        for (Member member1 : list) {
            AmbassadorRewards ambassadorRewards = new AmbassadorRewards();
            ambassadorRewards.setIsNewRecord(true);
            ambassadorRewards.setId(UUIDUtils.orderUuid(4));
            ambassadorRewards.setU(member1);
            BigDecimal total = new BigDecimal(member1.getTcPoint()).add(new BigDecimal(member1.getFgPoint()));
            // 小于10万
            if (total.compareTo(new BigDecimal("100000")) < 0) {
                // 奖励2%
                BigDecimal bigDecimal = new BigDecimal(member1.getTcPoint() + member1.getFgPoint()).multiply(new BigDecimal("0.02")).setScale(2, RoundingMode.HALF_UP);
                BigDecimal add = new BigDecimal(member1.getMasterReward()).add(bigDecimal);
                member1.setMasterReward(add.toString());
                // 大使奖励记录添加
                ambassadorRewards.setMoney(bigDecimal.toString());
            }
            // 10万 - 20万
            if (total.compareTo(new BigDecimal("100000")) >= 0 && total.compareTo(new BigDecimal("200000")) < 0) {
                // 奖励3%
                BigDecimal bigDecimal = new BigDecimal(member1.getTcPoint() + member1.getFgPoint()).multiply(new BigDecimal("0.03")).setScale(2, RoundingMode.HALF_UP);
                BigDecimal add = new BigDecimal(member1.getMasterReward()).add(bigDecimal);
                member1.setMasterReward(add.toString());
                // 大使奖励记录添加
                ambassadorRewards.setMoney(bigDecimal.toString());
            }
            // 20万 - 40万
            if (total.compareTo(new BigDecimal("200000")) >= 0 && total.compareTo(new BigDecimal("400000")) < 0) {
                // 奖励4%
                BigDecimal bigDecimal = new BigDecimal(member1.getTcPoint() + member1.getFgPoint()).multiply(new BigDecimal("0.04")).setScale(2, RoundingMode.HALF_UP);
                BigDecimal add = new BigDecimal(member1.getMasterReward()).add(bigDecimal);
                member1.setMasterReward(add.toString());
                // 大使奖励记录添加
                ambassadorRewards.setMoney(bigDecimal.toString());
            }
            // 40万 - 80万
            if (total.compareTo(new BigDecimal("400000")) >= 0 && total.compareTo(new BigDecimal("800000")) < 0) {
                // 奖励5%
                BigDecimal bigDecimal = new BigDecimal(member1.getTcPoint() + member1.getFgPoint()).multiply(new BigDecimal("0.05")).setScale(2, RoundingMode.HALF_UP);
                BigDecimal add = new BigDecimal(member1.getMasterReward()).add(bigDecimal);
                member1.setMasterReward(add.toString());
                // 大使奖励记录添加
                ambassadorRewards.setMoney(bigDecimal.toString());
            }
            // 80万 - 120万
//            if (total >= 800000 && total < 1200000) {
            if (total.compareTo(new BigDecimal("800000")) >= 0 && total.compareTo(new BigDecimal("1200000")) < 0) {
                // 奖励6%
                BigDecimal bigDecimal = new BigDecimal(member1.getTcPoint() + member1.getFgPoint()).multiply(new BigDecimal("0.06")).setScale(2, RoundingMode.HALF_UP);
                BigDecimal add = new BigDecimal(member1.getMasterReward()).add(bigDecimal);
                member1.setMasterReward(add.toString());
                // 大使奖励记录添加
                ambassadorRewards.setMoney(bigDecimal.toString());
            }
            // 120万 - 200万
            if (total.compareTo(new BigDecimal("1200000")) >= 0 && total.compareTo(new BigDecimal("2000000")) < 0) {
//            if (total >= 1200000 && total < 2000000) {
                // 奖励7%
                BigDecimal bigDecimal = new BigDecimal(member1.getTcPoint() + member1.getFgPoint()).multiply(new BigDecimal("0.07")).setScale(2, RoundingMode.HALF_UP);
                BigDecimal add = new BigDecimal(member1.getMasterReward()).add(bigDecimal);
                member1.setMasterReward(add.toString());
                // 大使奖励记录添加
                ambassadorRewards.setMoney(bigDecimal.toString());
            }
            // 200万 - 300万
            if (total.compareTo(new BigDecimal("2000000")) >= 0 && total.compareTo(new BigDecimal("3000000")) < 0) {
//            if (total >= 2000000 && total < 3000000) {
                // 奖励8%
                BigDecimal bigDecimal = new BigDecimal(member1.getTcPoint() + member1.getFgPoint()).multiply(new BigDecimal("0.08")).setScale(2, RoundingMode.HALF_UP);
                BigDecimal add = new BigDecimal(member1.getMasterReward()).add(bigDecimal);
                member1.setMasterReward(add.toString());
                // 大使奖励记录添加
                ambassadorRewards.setMoney(bigDecimal.toString());
            }
            // 300万 - 450万
            if (total.compareTo(new BigDecimal("3000000")) >= 0 && total.compareTo(new BigDecimal("4500000")) < 0) {
//            if (total >= 3000000 && total < 4500000) {
                // 奖励9%
                BigDecimal bigDecimal = new BigDecimal(member1.getTcPoint() + member1.getFgPoint()).multiply(new BigDecimal("0.09")).setScale(2, RoundingMode.HALF_UP);
                BigDecimal add = new BigDecimal(member1.getMasterReward()).add(bigDecimal);
                member1.setMasterReward(add.toString());
                // 大使奖励记录添加
                ambassadorRewards.setMoney(bigDecimal.toString());
            }
            // 450万
            if (total.compareTo(new BigDecimal("4500000")) >= 0) {
                // 奖励9%
                BigDecimal bigDecimal = new BigDecimal(member1.getTcPoint() + member1.getFgPoint()).multiply(new BigDecimal("0.1")).setScale(2, RoundingMode.HALF_UP);
                BigDecimal add = new BigDecimal(member1.getMasterReward()).add(bigDecimal);
                member1.setMasterReward(add.toString());
                // 大使奖励记录添加
                ambassadorRewards.setMoney(bigDecimal.toString());
            }
            memberService.reward(member1);
            ambassadorRewardsService.reward(ambassadorRewards);
        }
        log.info("执行大使奖励定时任务");
    }
}
